package mspbots.hubspot.client.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import mspbots.hubspot.common.RabbitKeys;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * com.mspbots.sync.config.RabbitConfigurer
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/29
 */
@Configuration
@RequiredArgsConstructor
public class RabbitConfiguration {

    private final AmqpAdmin amqpAdmin;

    @PostConstruct
    public void initialization() {
        Exchange exchange = ExchangeBuilder.directExchange(RabbitKeys.BASE_EXCHANGE_NAME).build();
        this.amqpAdmin.declareExchange(exchange);

        Map<String, Object> args = new HashMap<>(4);
        args.put("x-message-ttl", 1000 * 60 * 60 * 24);
        Queue queue = QueueBuilder.durable("hub-spot.bad.message")
                .withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.BAD_MESSAGE_QUEUE_KEY).noargs());


        args = new HashMap<>(2);
        args.put("x-dead-letter-exchange", RabbitKeys.BASE_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key", RabbitKeys.BAD_MESSAGE_QUEUE_KEY);

        queue = QueueBuilder.durable("hub-spot.distributed.task.sync")
                .withArguments(args).build();
        this.amqpAdmin.deleteQueue(queue.getName());
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.DISTRIBUTED_TASK_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("hub-spot.logs.task.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.LOGS_TASK_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("hub-spot.tickets.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.TICKETS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("hub-spot.companies.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.COMPANIES_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("hub-spot.contacts.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.CONTACTS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("hub-spot.entity-properties.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.PROPERTIES_SYNC_QUEUE_KEY).noargs());

    }

    @Bean
    public Jackson2JsonMessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }


}
